package xyz.hujf.base.join;

/**
 * @author Hujf
 * @title: ThreadJoin2
 * @date 2021-04-2119:28
 * @description: TODO
 */
public class ThreadJoin2 {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
           System.out.println("thread 1 run");
            while(true){

            }
        });
        Thread main = Thread.currentThread();

        Thread thread1 = new Thread(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            main.interrupt();
        });

        thread.start();
        thread1.start();
        //main 线程调用了thread的join时会被阻塞  等待1 2 执行完成 但是 1是死循环 所有会一直在里面运行 所有还是先执行到 26行打断方法
        //线程mian调用了 thread线程的join方法  而又被1线程调了 interrupt方法 所以会报错
        try {
            thread.join();
        }catch (Exception e){
            System.out.println(main+" "+e);
        }

    }
}
